package com.zhuang.mycodeproject.poiUtil;

import org.springframework.stereotype.Controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Controller
public class PoiController {

    public String exportExcel() throws Exception {
        System.out.println("---------------");

        // 初始化HttpServletResponse对象
        //HttpServletResponse response = ServletActionContext.getResponse();

        // 定义表的标题
        String title = "员工列表一览";

        //定义表的列名
        String[] rowsName = new String[]{"员工编号", "姓名", "性别"};

        //定义表的内容
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        List<Person> listPerson = Person.getListData();
        for (int i = 0; i < listPerson.size(); i++) {
            Person per = listPerson.get(i);
            objs = new Object[rowsName.length];
            objs[0] = per.getNo();
            objs[1] = per.getName();
            objs[2] = per.getSex();

            //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            //String date = df.format(per.getJobtime());
            dataList.add(objs);
        }

        // 创建ExportExcel对象
        ExportExcel ex = new ExportExcel(title, rowsName, dataList);

        // 输出Excel文件
        try {
            /*OutputStream output = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition",
                    "attachment; filename=PersonList.xls");
            response.setContentType("application/msexcel");*/
            FileOutputStream output = new FileOutputStream("E:/myexcel.xlsx");
            ex.export(output);
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "tolist";// 返回列表显示
    }

    public Integer getSum(Integer a, Integer b) {
        System.out.println("------a+b>>>>" + (Optional.ofNullable(a).orElse(0) + Optional.ofNullable(b).orElse(0)));
        return Optional.ofNullable(a).orElse(0) + Optional.ofNullable(b).orElse(0);
    }


}